home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / mac / Mac_Files / Software Utilities / NN PreProcessing / FINUTL2.TXT < prev    next >
Text File  |  1992-09-30  |  19KB  |  333 lines

  1. Neural Network Pre-Processing and Analysis Routines
  2. for
  3. Financial Forecasting
  4.  
  5. Revised 25-Sep-92 to include Non-Linear Dynamical Systems Routines
  6.  
  7. These routines are translations and extensions of several routines in the Wing-Z script published in Advanced Technology for Developers, Volume 1, August 1992 issue, as well as Wing-Z scripts for Non-Linear Systems Analysis published in the September 1992 issue.  These routines are for the sole use of ATD disk subscribers and may not be copied, re-distributed, sold, posted to a bulletin board or in any other way provided to third parties or embedded in other systems without the express written permission of the publisher of Advanced Technology for Developers.
  8.  
  9. Copyright (c) 1992, Advanced Technology for Developers.  All rights reserved.
  10. These programs were developed without any government or public sector funding.  All of these programs were written or translated from Wing-Z scripts by Casimir C. Klimasauskas.
  11.  
  12.  
  13. DISCLAIMER
  14.  
  15. These programs are provided on an "as is" basis.  The publisher of Advanced Technology for Developers makes no warranties as to fitness, operation, or results which may be derived from the use of these programs.  The publisher of Advanced Technology for Developers makes no claim or warranty that these programs do not infringe any domestic or foreign patents or copyrights.  The limit of liabilities of the publisher of Advanced Technology for Developers is the price paid for the diskette containing these routines.
  16.  
  17.  
  18. Contents
  19.  
  20.  
  21. System Requirements
  22. Creating a Financial Data-Base
  23. Creating network-ready data
  24. Analyzing the output of a Neural Network
  25.  
  26.  
  27.  
  28. System Requirements
  29.  
  30. All of the programs provided with this document were compiled and linked using the Zortech "C/C++" compiler and developers routines.  The large memory model was used.  A floating point co-processor is not required.  In some circumstances, it may be advisable to re-compile the programs using in-line floating point, and 386 protected mode.  This will result in substantially faster processing and the ability to deal with larger data-bases.  The basic requirements are:
  31.  
  32.     IBM PC or compatible with 286, 386, or 486 processor
  33.     500K application memory
  34.     math co-processor (for 286 or 386) required
  35.  
  36. NOTE: due to the computationally intensive nature of the non-linear dynamical system routines, the code has been compiled with in-line floating point instructions.  As such, a math co-processor is REQUIRED.  The name of the analysis program has been changed to "FINPRE2.EXE".  The linear regression model in FINRTN.C has also been changed to improve stability of the numeric algorithm.
  37.  
  38. With 500K of application memory, it is possible to generate a total of 100 fields, each covering 750 trading days.  This means that the program (FINPRE2.EXE) can load a data-base of 20 fields over 750 trading days, and produce 80 transformations of that data.
  39.  
  40. If you decide to change the source code in any way, the following compiler will be required or its equivalent:
  41.  
  42.     Zortech C/C++ Compiler (Symantec)
  43.  
  44. These programs have been written with the intent of portability across multiple computer systems.  They should port quickly to most work-stations.  If there are difficulties in this area, High-Tech Communications may be able to provide assistance on a fee basis.
  45.  
  46.  
  47. Creating a Financial Data-Base
  48.  
  49. FINBLD.EXE is a DOS program designed to process a Compu-Serve session file and produce a data-base as an output.  This disk contains a file "finsess.csv" which contains a sample Compu-Serve session.  This session is used to illustrate the various steps in the process.  The session collects 750 trading days of information for the commodities TBOND and TNOTE.  The beginning and the end of the file were edited to remove log-on information, however, this is not necessary.  The data was collected by using a standard communications program to connect to Compu-Serve.  The "log session" option of the communication program was used to record each character received by the program.  Communication was in full-duplex mode with remote echo.  Other methods may be used to collect the data.  However the final format must be as follows:
  50.  
  51.  
  52. ticker: symbol
  53. date    label    label    label    label
  54. -----    -----    -----    -----    ------
  55. ....
  56.  
  57. All data must be in fixed width columns.  The width of the columns is determined by the position of the dashes under the labels.  The line immediately above the dashes is used for labels.  Any number of lines can come between the line which contains "ticker: symbol" and the line with dashes.  The first column MUST always be the date.  The dates must be in ascending order.
  58.  
  59. As shown with "finsess.csv", multiple securities can be in each session.  A data-base is created by:
  60.  
  61.     C>del fin.db
  62.  
  63.     C>FINBLD fin.db finsess1.csv finsess2.csv ...
  64.  
  65. First, make sure that any existing data-base of the name has been removed.  A warning message will be given that the program cannot find the data-base "fin.db", then one which indicates that is creating it.  Any number of session files (up to the maximum line size allowed by DOS) can be processed.  Each session file may contain multiple securities.  Each security is merged into the data-base column-wise in alphabetical order.  Within a particular security, the columns are arranged in alphabetical order by label.  When a session is processed with contains a column with a label not already in the data-base, this is automatically added.  Date synchronization is automatically handled across multiple securities.
  66.  
  67. The translation process automatically handles conversion of dates in any of the following forms.  Dates are always stored in the data-base in the "preferred format".  This is compatible with Excel and several other spread-sheets.
  68.  
  69.     mm-dd-yy
  70.     mm/dd/yy    (preferred format)
  71.     dd-MMM-yy
  72.  
  73. It also can handle price data in any of the following formats:
  74.  
  75.     dd.ddd        - decimal
  76.     dd dd/ddd    - fractional
  77.  
  78. All data is converted to decimal format for internal processing and external storage.  Special tags to indicate the reason for missing data (hol, na, nd, n/a) are all translated to the value zero.  A maximum of 512 fields, including the date field, can be merged.  This is limited by the define "MAXFIELD".  The format of the data-base as created or updated is designed for easy access by Excel or other spread-sheets which use tab-separated data-fields.  The format of the generated data-base is:
  79.  
  80.     \t        ticker\t        ticker\t        ticker...
  81.     date\t        label\t        label\t        label...
  82.     01/01/88        80.55        81.22        ...
  83.  
  84. The program is smart enough to be able to identify when one block of data ends and another begins.  It is also able to recognize and ignore Compu-Serve session data.  It is possible to log the results of an interactive session to a file and then process the resulting log file directly.
  85.  
  86. To create a data-base named "fin.db" from the session "finsess.csv":
  87.  
  88.     C>del fin.db        - make sure the data-base does not exist
  89.     C>FINBLD fin.db finsess.csv
  90.  
  91. The resulting data-base will contain 750 trading days of the two securities TBOND and TNOTE.  The file "finsess.csv" is provided for illustrative purposes.
  92.  
  93. NOTE: it is possible to process more than one session at a time.  The data-base is updated by:
  94.  
  95.     C>FINBLD fin.db finupd.csv
  96.  
  97. Where "finupd.csv" is the session to use to update the data-base.  An update can add additional columns or extend the number of trading days in the system.
  98.  
  99.  
  100.  
  101. Creating Network Ready Data
  102.  
  103.  
  104. FINPRE2 - Financial Pre-processing Program
  105.  
  106. The financial transformation program, "FINPRE2" takes the data-base created and updated by "FINBLD" and provides the ability to generate a variety of transformations using the "C" equivalents of the routines (finrtn.c) described in the accompanying article.  To make it easier to work with the program, a command line parser (fincmd.c) is used to handle a very simple command language.  For the command portion, it is only necessary to type the first few letters of the command which are required to uniquely identify it.  Other features of the command line parser:
  107.  
  108.     ??            - prints out the entire command set syntax
  109.     ?lo            - prints out the value of all variables which begin with lo
  110.     lo?            - prints out the command syntax for commands that being
  111.                     with lo.
  112.  
  113. In addition to providing the capability of generating network data, the FINPRE2 program is also capable of analyzing the output of a neural network and integrating it with a trading strategy.  This is done with the commands described under the "analysis" section.
  114.  
  115. Command Set:
  116.  
  117.     load        data_base_name [,start [,end]]    - loads the master data-base
  118.     saveall        data_base_name [,start [,end]]    - creates a new data-base
  119.                                    including transform data
  120.     interpolate                        - interpolate "zero" values
  121.     scale        min,max                - scale transformed data
  122.     transform    data_file [,start_date [,end_date]]    - creates a transform file
  123.     log        filename                - log all commands
  124.     dictionary                        - print out current dictionary
  125.     quit                            - terminates the program
  126.  
  127. Transformations commands:
  128.  
  129.     movingaverage    field,days        - moving average
  130.     rsi            field,days        - relative strength index
  131.     stochastic        field,days[,smooth]    - smoothed stochastic
  132.     volatility        field,days        - volatility
  133.     highlow        h-field,l-field,days    - high/low volatility
  134.     slope            [x-field],y-field,days    - regression slope
  135.     intercept        [x-field],y-field,days    - regression intercept
  136.     correlation        x-field,y-field,days    - rolling correlation
  137.     raw            field            - copy raw data (with lag/lead)
  138.  
  139.     field:    ticker.field[ offset ]
  140.  
  141.         example:    tbond.high        - current "t-bond" high value
  142.                 tbond.high[0]        - ...ditto...
  143.                 tbond.settle[-2]    - t-bond settle value 2-days ago
  144.  
  145. Analysis:
  146.  
  147.     network        file,start_date        - load ".nnr" file at start_date
  148.  
  149.     report            {all,summary,transactions} - specify what to report
  150.     long            lowlimit,highlimit    - long range
  151.     short            lowlimit,highlimit    - short range
  152.                             - out of market is anything 
  153.                               between short high & long low.
  154.      capital            startingcapital        - starting capital
  155.     price            field            - field which contains closing price
  156.  
  157.     analyze        startdate[,field]    - analyze results from startdate
  158.                             - put analysis in "log" file
  159.                             - if more than one field, specify #
  160.  
  161.     NOTE: start = start date; end = end date.  This allows for loading of a portion of the data-base or saving of a portion of it.
  162.  
  163. A sample session to create a test set and training set follows:
  164.  
  165.     C>FINPRE2.EXE
  166.     Command> log fin.log                ! log commands to file
  167.     Command> load fin.db                ! load the data-base 
  168.     Command> interpolate                ! interpolate holes
  169.     Command> raw tnote.settle[-5]            ! five days prior
  170.     Command> raw tnote.settle[-4]            ! four days prior
  171.     Command> raw tnote.settle[-3]            ! three days prior
  172.     Command> raw tnote.settle[-2]            ! two days prior
  173.     Command> raw tnote.settle[-1]            ! yesterday
  174.     Command> raw tnote.settle                ! today
  175.     Command> rsi   tnote.settle[-5],10            ! five day shifted rsi
  176.                                 !   over 10 trading days
  177.     Command> raw tnote.settle[1]            ! predict tomorrow's price
  178.     Command> scale -1,1                ! scale all data
  179.     Command> trans wrktrain.nna,0/0/0,12/31/87    ! generate training file
  180.     Command> trans wrktest.nna,1/1/88,12/31/99    ! generate test file
  181.     Command> quit                    ! terminate the session
  182.  
  183.  
  184. Non-Linear Dynamic Systems Analysis
  185.  
  186. There are three sub-sections which implement dynamical system analysis.  Each of these has its own set of parameters and commands which are independent from the rest of the program.  These routines were described in the September 1992 issue of Advanced Technology for Developers.  The WingZ scripts were translated to "C".  In some instances, the algorithms were re-written to improve the performance of the resulting system.  The three commands are:
  187.  
  188. hurst            - compute the Hurst dimension of a series
  189. correlationdim        - compute the correlation dimension of a series
  190. lyapunov        - compute the largest lyapunov exponent
  191.  
  192. Each of these commands activates a "subsection" which is described below.  For more information about how to set the parameters, see the article in the September 1992 Advanced Technology for Developers or "Chaos and Order in the Capital Markets" by Edgar E. Peters.  Each of these analysis routines has been coded and to the best knowledge of the translator, Casimir C. Klimasauskas, they accurately reflect the correct algorithms.  However, they have not been tested against bench-mark data.
  193.  
  194. Hurst Analysis:
  195.  
  196. series    ticker.field,start_date,end_date
  197.         - select the series to analyze
  198. startwindowsize    size    - starting window size (# of items)
  199. deltawindowsize    size    - amount to increment window by each iteration.
  200. iterations    niter    - set the number of iterations.  100 is a good number.
  201. derivative    no    - if yes, use the derivative of the series
  202. printfile    name,type
  203.         - re-direct printed output to file "name"
  204. type    name    - type this file to the console
  205. go        - perform the Hurst analysis.
  206.  
  207. Correlation Dimension:
  208.  
  209. series    ticker.field,start_date,end_date
  210.         - select the series to analyze
  211. embedding    dim    - nominal embedding dimension
  212. tau    steps    - number of time steps between dimensions for re-constructing the phase space
  213. iterations    niter    - set the number of iterations.  100 is a good number.
  214. printfile    name,type
  215.         - re-direct printed output to file "name"
  216. type    name    - type this file to the console
  217. go    s,e    - perform the correlation dimension analysis.  If "s" and "e" are present, perform the analysis setting the embedding dimension to all values from "s" through "e".  Otherwise, just use the embedding dimension.
  218.  
  219. Lyapunov Exponent
  220.  
  221. series    ticker.field,start_date,end_date
  222.         - select the series to analyze
  223. embedding    dim    - nominal embedding dimension
  224. tau    steps    - number of time steps between dimensions for re-constructing the phase space
  225. minscale    min    - minimum radius for computations
  226. maxscale    max    - maximum radius for computations
  227. evolve    iter    - number of time steps in each evolution
  228. lagminimum    steps    - minimum lag for "near" points (typically 10-12).
  229. printfile    name,type
  230.         - re-direct printed output to file "name"
  231. type    name    - type this file to the console
  232. go    s,e    - perform the Lyapunov Exponent analysis.  If "s" and "e" are present, perform the analysis setting the embedding dimension to all values from "s" through "e".  Otherwise, just use the embedding dimension.
  233.  
  234. For printed output, the types of output file are:  formatted (formatted ascii text), lotus (comma separated values, quoted literals), excel (tab separated values).  These formats make it easy to import the results into a spreadsheet or word-processing program.
  235.  
  236. A sample session to analyze the time series "tnote.settle" is shown below.  Prior to this step, it is necessary to construct a data-base as described previously (FINBLD above).
  237.  
  238.     C>FINPRE2.EXE
  239.     Command> log fin.log            ! log commands to file
  240.     Command> load fin.db            ! load the data-base
  241.     Command> interpolate            ! interpolate holes ***
  242.     Command> hurst                ! hurst analysis
  243.     Hurst> series tnote.settle            ! series to analyze
  244.     Hurst> printfile hurst.csv,lotus        ! write results in lotus-format
  245.     Hurst> derivative = yes            ! compute derivative
  246.     Hurst> iterations = 50            ! 50 iterations
  247.     Hurst> startwindow = 5            ! start window size (1 week)
  248.     Hurst> deltawindow = 5            ! increment by one week
  249.     Hurst> go                    ! execute hurst calculations
  250.     Hurst> type                    ! type the print file
  251.     Hurst> quit                    ! exit from hurst analysis
  252.  
  253.     Command> correlationdim            ! enter corr dim section
  254.     CorrDim> series tnote.settle        ! series to analyze
  255.     CorrDim> print corrdim.tab,excel        ! tab separated values
  256.     CorrDim> tau = 1                ! one day per phase space
  257.     CorrDim> iterations=100            ! 100 iterations
  258.     CorrDim> go 2,6                ! run for embed=2 to 6
  259.     CorrDim> quit                ! return to main menu
  260.  
  261.     Command> lyapunov            ! enter lyapunov analysis
  262.     Lyapunov> series tnote.settle        ! series to analyze
  263.     Lyapunov> print lyapun.txt,formatted    ! formatted text
  264.     Lyapunov>  go 2,6                ! run for embed=2 to 6
  265.     Lyapunov> quit                ! exit from lyapunov analysis
  266.     Command> quit                ! terminate finpre2.
  267.  
  268.  
  269. *** It is very important to interpolate the holes in the data-base.  Failure to do so will result in incorrect processing by the analysis routines.
  270.  
  271. Analyzing the Output of a Neural Network
  272.  
  273.  
  274. A sample session to analyze the output of the network follows:
  275.  
  276.     C>FINPRE2.EXE
  277.     Command> log fina.log                ! log results of analysis
  278.     Command> load fin.db                ! load the data-base
  279.     Command> network wrktest.nnr,1/1/88        ! load the network output
  280.     Command> price tnote.settle            ! price
  281.     Command> report all                ! summary & transactions
  282.     Command> capital 50000                ! starting capital
  283.     Command> analyze                    ! do analysis
  284.     Command> quit                    ! exit the program
  285.  
  286.  
  287. Appendix A - Technical Notes
  288.  
  289. This section contains some brief notes on internal data-structures.  It is not designed as an indepth description of how the system works.
  290.  
  291. Program files:
  292.     fin.h        - defines and proto-types
  293.     fincmd.h    - defines and proto-types for command line processor
  294.     finbld.c    - routines to generate the FINBLD.exe (main program)
  295.     fincmd.c    - command line parser
  296.     findic.c    - dictionary management
  297.     finevl.c    - neural network evaluation routines translated from WingZ
  298.     finpre2.c    - pre-processing driver (main program)
  299.     finrtn.c    - financial routines translated from WingZ
  300.     finnlds.c    - non-linear dynamical system analysis routines from WingZ
  301.     makefile    - program to build the executable programs
  302.  
  303. Internal Structures:
  304.  
  305.     Global Data:
  306.         FirstDate    - starting date of the master data-base
  307.         LastDate    - ending date of the master data-base
  308.         VectorI    - # of items in each vector
  309.  
  310.     Field Array (maximum of 1024 fields):
  311.         FTickCP    - pointer to ticker symbol (dictionary)
  312.         FSymCP    - field within ticker
  313.         FDataFP    - data vector for this field
  314.         FFlagsI    - flags: data-base, transform, interpolated
  315.  
  316.         for generated fields:
  317.         FTickCP is "transXXX" indicating the transform number
  318.         FSymCP is the transform type itself
  319.  
  320.         for network output fields:
  321.         FTickCP is "network" indicating from network
  322.         FSymCP is "fieldXXX" to indicate the specific field
  323.  
  324.     When a field name is parsed, it fills in the following structure:
  325.         FTickCP    - ticker symbol
  326.         FSymCP    - field within ticker
  327.         FOffsetI    - offset +/- for lead/lag generation
  328.  
  329. Neural Network Pre-Processing & Analysis Routines        page {PAGE|1} of 9
  330. Copyright (c) 1992, High-Tech Communications        revised 25-Sep-92
  331.  
  332.  
  333.